import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';

class TransactionsCollection extends Mongo.Collection {
  insert(doc, callback) {
    const ourDoc = doc;
    ourDoc.createdAt = ourDoc.createdAt || new Date();
    const result = super.insert(ourDoc, callback);
    return result;
  }
  update(selector, modifier) {
    const result = super.update(selector, modifier);
    return result;
  }
  remove(selector) {
    const result = super.remove(selector);
    return result;
  }
}

export const Transactions = new TransactionsCollection('transactions');

// Deny all client-side updates since we will be using methods to manage this collection
Transactions.deny({
  insert() { return true; },
  update() { return true; },
  remove() { return true; },
});

Transactions.schema = new SimpleSchema({
  transactionId: {
    type: String,
  },
  confirmId:{
    type: String,
    optional: true,
  },
  title: {
    type: String,
    max: 100,
  },
  desc: {
    type: String,
    max: 1000,
    optional: true,
  },
  createdAt: {
    type: Date,
  },
  userFrom:{
    type: String,
  },
  avatarFrom:{
    type: String,
    optional: true,
  },
  titleFrom:{
    type: String,
    optional: true,
  },
  //utype字段标识用户的类型，枚举：用户、项目
  //add by jiangbuyun
  utypeFrom:{
    type: String,
    optional: true,
  },

  confirmAt: {
    type: Date,
    optional: true,
  },
  userTo:{
    type: String,
  },
  avatarTo:{
    type: String,
    optional: true,
  },
  titleTo:{
    type: String,
    optional: true,
  },
  //utype字段标识用户的类型，枚举：用户、项目
  //add by jiangbuyun
  utypeTo:{
    type: String,
    optional: true,
  },
  paynumber:{
    type: String,
    optional: true,
  },
  status:{
    type: String,
    optional: true,
  },
  tags:{ 
    type: String,
    optional: true,
  },
  chaincode:{ 
    type: String,
    optional: true,
  },
  data:{
    type: Object,
    optional: true,
    blackbox: true,
  }
});

Transactions.attachSchema(Transactions.schema);

Transactions.publicFields = {
  transactionId: 1,
  confirmId: 1,
  title: 1,
  desc: 1,
  createdAt: 1,
  userFrom: 1,
  confirmAt: 1,
  userTo: 1,
  status: 1,
  tags: 1,
  avatarFrom: 1,
  titleFrom: 1,
  //add by jiangbuyun
  utypeFrom: 1,
  avatarTo: 1,
  titleTo: 1,
  //add by jiangbuyun
  utypeTo: 1,
  paynumber:1,
  data: 1,
};



if (Meteor.isServer) {
  // This code only runs on the server
  Meteor.publish('transactions_recent', function(pm) {
     let uid = GV_defaultUser.userId;
    const query = {
        $or: [{ userTo: uid },{userFrom: uid}],
      };
    let result = Transactions.find(query, { fields: Transactions.publicFields,sort: { createdAt: -1 } });
    return result;
   }
 );

   Meteor.publish('transactions_history', function(pm) {
     //检索两个账号之间的交易历史, 还是检索一个账户的所有交易历史
    let qor = pm.tuid?[{ userTo: pm.cuid, userFrom: pm.tuid},{userFrom: pm.cuid, userTo: pm.tuid}]
        : [{ userTo: pm.cuid },{userFrom: pm.cuid}];
    const query ={
         $or: qor,
    }
    //console.log(query);
    return Transactions.find(query, { fields: Transactions.publicFields});
   }
 );

  Meteor.publish('project_history', function(pm) {
     //检索两个账号之间的交易历史, 还是检索一个账户的所有交易历史
    
    const query ={"data.projectname":pm.pname}
    //console.log(query);
    return Transactions.find(query, { fields: Transactions.publicFields});
   }
 );

}